package com.asa.jihe.ui;

import java.awt.Button;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JButton;

import com.asa.jihe.B.Convex_Hull;
import com.asa.jihe.D.GraphSplitTriangle;
import com.asa.jihe.E.Vorinoi;
import com.asa.jihe.utils.Copy;
import com.asa.jihe.utils.Graph;
import com.asa.jihe.utils.Line;
import com.asa.jihe.utils.Point;
import com.asa.ui.spirit.Circle;

/**
 * 封装mvc
 * @author Administrator
 *
 */
public class JiHeVisualizer {

	private Circle[] circles;
	private JiHeFrame frame;
	
	
	private List<Point> points = new LinkedList<>();

	
	private List<Line> lines = new LinkedList<>();

	private String operation = "";
	private String operationchange = "";

	
	private boolean addpoint;
	private boolean addline;

	
	
	
	public JiHeVisualizer(final int sceneWidth,final int sceneHeight,int N) {
		super();
		
		
		
		
		circles = new Circle[N];
		
		final Button voronoiButton = new Button("Voronoi_Diagram");
        voronoiButton.setActionCommand("voronoi");
        final Button convexButton = new Button("Convex_Hull");
        convexButton.setActionCommand("convex");
        final Button splitriangleButton = new Button("Spli_Triangle");
        splitriangleButton.setActionCommand("splitriangle");
        
        
        
        final JButton clearButton = new JButton("Clear");
        clearButton.setActionCommand("clear");
		final JButton addpointButton = new JButton("addpoint");
		addpointButton.setActionCommand("addpoint");
		final JButton drawgraphButton = new JButton("drawgraph");
		drawgraphButton.setActionCommand("drawgraph");
        
		
		convexButton.addActionListener(new JiHeActionListener());
		splitriangleButton.addActionListener(new JiHeActionListener());
        voronoiButton.addActionListener(new JiHeActionListener());
        clearButton.addActionListener(new JiHeActionListener());
        addpointButton.addActionListener(new JiHeActionListener());
        drawgraphButton.addActionListener(new JiHeActionListener());

        
        
		EventQueue.invokeLater(new Runnable() {//用java事件分发（开了个线程）的方法来创建对话框
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				frame = new JiHeFrame("wellcome", sceneWidth, sceneHeight);
				frame.addKeyListener(new JiHeKeyListener());
				frame.addMouseListener(new JiHeMouseListener());
				
				frame.add(convexButton);
				frame.add(splitriangleButton);
				frame.add(voronoiButton);

				
				frame.add(clearButton);
				frame.add(addpointButton);
				frame.add(drawgraphButton);
				
				

				new Thread(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						runmiage();
					}

					
				}).start(); ;
			
				
			}
		});

		
		
	}
	
	private void runmiage() {

		while(true) {
			
			
			
			//绘制数据
			frame.render(circles,points,lines,operation,operationchange);
			
			
		}
	}
	
	
	boolean isAnimated;
	private class JiHeKeyListener extends KeyAdapter{

	
		@Override
		public void keyReleased(KeyEvent event) {
			// TODO Auto-generated method stub
			super.keyReleased(event);
			if (event.getKeyChar()==' ') {
				isAnimated = !isAnimated;
			}
		
		}
		
	}
	
	private class JiHeMouseListener extends MouseAdapter{
		
		
		@Override
		public void mousePressed(MouseEvent event) {
			// TODO Auto-generated method stub
//			event.translatePoint(0, -(frame.getBounds().height-frame.getCanvasHeight()));

			event.translatePoint(0, -20);
			if (addpoint) {
				int x = event.getPoint().x;
				int y = event.getPoint().y;
				
				Point point = new Point();
				point.x = x;
				point.y = y;

				points.add(point);
			}
			
			
			
			
			
		}
		
	}
	
	private class JiHeActionListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			
			addpoint = false;
			operation = "";
		   	operationchange = "";
			
	        String actioncom = e.getActionCommand();

			if (actioncom.equals("clear")) {
				System.out.println(actioncom);
				
				points.clear();
				lines.clear();


			}
		
			if (actioncom.equals("addpoint")) {
				System.out.println(actioncom);
				addpoint = true;
				operation = "addpoint";
			}
			
			if (actioncom.equals("drawgraph")) {
				System.out.println(actioncom);
				addline = true;
				operation = "drawgraph";
				lines.clear();
				
				List<Line> graphtopoints = Copy.graphtopoints(points);
				
				lines.addAll(graphtopoints);
				
				
				
			}
			
			
		   if(actioncom.equals("voronoi")){   
		          System.out.println(actioncom);
		           
		          List<Line> func1 = Vorinoi.func1(points);
		          System.out.println("总共有多少条线"+func1.size());
		          lines.clear();
		          lines.addAll(func1);
		          
		          
		          for (int i = 0; i < lines.size(); i++) {
					System.out.println(lines.get(i));
				}
		          
		          
		   }
			

		   if(actioncom.equals("convex")){   
			   	  operationchange = "convex";
		          
//		          List<Point> method2 = Convex_Hull.method2(pionts);
//		          List<Line> graphtopoints = Copy.graphtopoints(method2);
		          
		          List<Line> graphtopoints = Convex_Hull.method0(points);
		          
		          lines.clear();
		          lines.addAll(graphtopoints);
		          
		          System.out.println(lines.size());
		          
		   }
		   
		   
		   if(actioncom.equals("splitriangle")){ 
			   operationchange = "splitriangle";
		       System.out.println(actioncom);
		       System.out.println(points.size());

		       Graph graph = new Graph();
		       graph.lines = Copy.graphtopoints(points);

		       List<Line> func2 = GraphSplitTriangle.func2(graph);//func1事实上func2才有问题，都有问题
		       System.out.println("结束了");
		       System.out.println(func2.size());

		       lines.addAll(func2);

		       System.out.println(func2.size());
		   }
		   
		}

	}
	
	
	
	
	
}
